package com.tencent.cloud.iov.util;

import android.content.Context;
import android.content.res.Resources;
import android.graphics.drawable.Drawable;
import android.support.annotation.ColorRes;
import android.support.annotation.DimenRes;
import android.support.annotation.DrawableRes;
import android.support.annotation.NonNull;
import android.support.annotation.StringRes;

/**
 * 资源工具类
 *
 * @author showxiao
 * @date 2018/6/15
 */

public class ResourcesUtils {

    private static Context sContext;

    private ResourcesUtils() {

    }

    public static void init(@NonNull Context context) {
        sContext = context.getApplicationContext();
    }

    public static Context getContext() {
        return sContext;
    }

    public static Resources getResources() {
        return sContext.getResources();
    }

    public static String getString(@StringRes int strResId) {
        return getResources().getString(strResId);
    }

    public static String getString(@StringRes int strResId, Object... formatArgs) {
        return getResources().getString(strResId, formatArgs);
    }

    public static int getDimensionPixelOffset(@DimenRes int resId) {
        return getResources().getDimensionPixelOffset(resId);
    }

    public static int getDimension(@DimenRes int resId) {
        return (int) getResources().getDimension(resId);
    }

    public static int getColor(@ColorRes int colorResId) {
        return getResources().getColor(colorResId);
    }

    public static Drawable getDrawable(@DrawableRes int drResId) {
        return getResources().getDrawable(drResId);
    }

    public static Drawable getDrawable(String name, String defType) {
        int resourceId = getResources().getIdentifier(name, defType, sContext.getPackageName());
        if (0 == resourceId) {
            return null;
        }
        return getResources().getDrawable(resourceId);
    }
}
